//
//  CJPictorialCellWebViewViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "CJPictorialCellWebViewViewController.h"
#import "PKRequestManager.h"
#import "CJPictorialCellWebViewModel.h"
#import "CJCellBottomView.h"


@interface CJPictorialCellWebViewViewController () <UIWebViewDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) UIWebView *webView;  //声明一个UIWebView

@property (nonatomic, strong) CJPictorialCellWebViewModel *WebViewModel;
@property (nonatomic, assign) CGFloat webViewOfsetY;  //记录webView偏移位置
@property (nonatomic, strong) CJCellBottomView *bottomView;

@end

@implementation CJPictorialCellWebViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
    [self requestData];

}

//请求数据
- (void)requestData {
    
    
    [PKRequestManager requestWithUrlString:[NSString stringWithFormat:@"http://morguo.com/forum.php?mod=viewthread&tid=%@&androidflag=1&appfrom=ios&iosversion=3.13&page=1",_tid] parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        _WebViewModel = [[CJPictorialCellWebViewModel alloc] init];
        [_WebViewModel setValuesForKeysWithDictionary:dataDic[@"data"]];
        
        self.title = _WebViewModel.subject;
        [self addWebView];
    } error:^(NSError *error) {
        
    }];

}

//添加webView
- (void)addWebView {
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_WebViewModel.webviewurl]];
    //初始化
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    
    
    //通过一个request 加载WebView
    [_webView loadRequest:request];
    
    _webView.delegate = self;
    _webView.scrollView.delegate = self;
    _webViewOfsetY = kNavigationBarHeight;
    [self.view addSubview:_webView];
    
 
    //加载下面评论视图
    [self addBottomView];
  
}


//加载下面评论视图
- (void)addBottomView {
    _bottomView = [[CJCellBottomView alloc] initWithFrame:CGRectMake(0, kScreenHeight -  25, kScreenWidth, 25)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    
    //点赞
    _bottomView.praiseLabel.text = @"赞";
    [_bottomView.praiseButton setImage:[UIImage imageNamed:@"parise_norml"] forState:(UIControlStateNormal)];
    
    //评论
    _bottomView.commentLabel.text = @"评论";
    [_bottomView.commentButton setImage:[UIImage imageNamed:@"comment"] forState:(UIControlStateNormal)];
    
    //回到顶部
    [_bottomView.moreButton setImage:[UIImage imageNamed:@"TOTop"] forState:(UIControlStateNormal)];
    [_bottomView.moreButton addTarget:self action:@selector(clickMoreButton:) forControlEvents:(UIControlEventTouchUpInside)];

    [self.view addSubview:_bottomView];
    
}


//webView将要加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

//webView已经加载
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
}

//视图在滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat y = scrollView.contentOffset.y;
    if (y > _webViewOfsetY) {
        _bottomView.hidden = YES;
    } else {
        _bottomView.hidden = NO;
    }
    
    _webViewOfsetY = y;
    
    
}


//回到顶部
- (void)clickMoreButton:(UIButton *)button {
    
    [_webView.scrollView setContentOffset:CGPointMake(0, 0)];
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
